#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
using namespace std;

//Definition for a binary tree node.
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode() : val(0), left(nullptr), right(nullptr)
	{
	}
	TreeNode(int x) : val(x), left(nullptr), right(nullptr)
	{
	}
	TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
	{
	}
};

class Solution {
        tuple<int, int, int> get_min_counts(TreeNode *root)
        {
                if(root == nullptr){
                        return {0, 0, 0};
                }

                auto [coinsr, cntr, mover] = get_min_counts(root->right);
                auto [coinsl, cntl, movel] = get_min_counts(root->left);
                int coins = coinsr + coinsl + root->val;
                int cnt = cntl + cntr + 1;
                int move = mover + movel + abs(cntr - coinsr) + abs(cntl - coinsl);
                return {coins, cnt, move};

        }
    public:
	int distributeCoins(TreeNode *root)
	{
                return get<3>(get_min_counts(root));
	}
};

int main()
{
	return 0;
}